﻿// Luis Massó Saiz, Tomas Martinez Sempere
// Popup Class
// Creation Date: 23- 1-2014

using System;
using System.IO;
using System.Collections.Generic;

namespace DamCommander
{
    class Popup
    {
        // Atributes
        protected int code;
        protected int width;
        protected int height;
        protected int x;
        protected int y;
        protected List<string> content;

        // Constructor
        public Popup(int code)
        {
            this.code = code;

            ReadFile(code);
            this.width = CalcWidth();
        }

        // Method
        // Read file text with strings to show
        // Charge the strings array
        protected void ReadFile(int code)
        {
            content = new List<string>();
            string path = "menuFiles/m0" + code + ".txt";
            StreamReader myFile;
            myFile = System.IO.File.OpenText(path);
            x = Convert.ToInt32(myFile.ReadLine());
            y = Convert.ToInt32(myFile.ReadLine());

            while (myFile.ReadLine() != null)
            {
                content.Add(myFile.ReadLine());
            }

            myFile.Close();
        }

        // This method return width of popup using
        // length of biggest string
        protected int CalcWidth()
        {
            int width = content[0].Length;

            for (int i = 1; i < content.Count; i++)
            {
                if (content[i].Length > width)
                {
                    width = content[i].Length;
                }
            }
            return width + 4;  // 4 for margin
        }


        protected void CalcHeigth(string[] content)
        {
            this.height = (25 - content.Length + 2) / 2;
        }

        // Using EnhancedConsole class this method write on screen
        // strings of text file 
        public void ShowPopup(string colorB, string colorF)
        {
            EnhancedConsole eC = new EnhancedConsole();
            string spaces = "";
            if (colorB == "")
                colorB = "blue";
            if (colorF == "")
                colorF = "white";

            for (int i = 0; i < width; i++)
            {
                spaces += " ";
            }
            eC.Write(x, y, spaces, colorF, colorB);


            for (int i = 0; i < content.Count; i++)
            {
                string sentence = "* " + content[i] + " *";   // ... habrá que borrar los asteriscos
                eC.Write(x, y + i + 1, sentence, colorF, colorB);   //Revisar y...

            }
            eC.Write(x, y + content.Count, spaces, colorF, colorB);

        }
    }
}
